#pragma once // 防止头文件被重复包含
#include <sys/epoll.h>
#include <unistd.h>

const uint32_t epollin = (EPOLLIN | EPOLLET);   // 读事件，边缘模式
const uint32_t epollout = (EPOLLOUT | EPOLLET); // 写事件，边缘模式
//const uint32_t epollin = EPOLLIN;   
//const uint32_t epollout = EPOLLOUT; 
static const int lapse_size = 1024;             // 作为epoll_create的参数，没有实际意义
int Scooping_size = 64;

class Epoll
{

public:
    Epoll()
    {
        _epoll_fd = epoll_create(lapse_size);
        if (dup2(_epoll_fd, 0) == -1)  //把epoll模型文件描述符重定向到0号
        {
            LOG(FATAL, "epoll模型的文件描述符重定向失败");
            exit(1);
        }

        _epoll_fd = 0;
    }

    ~Epoll()
    {
        if (_epoll_fd >= 0)
        {
            close(_epoll_fd); // 注销epoll模型
        }
    }

    int EpollWait(struct epoll_event *event)
    {
        int n = epoll_wait(_epoll_fd, event, Scooping_size, -1);
        return n;
    }

    int EpollCtl(int op, int fd, uint32_t event)
    {
        if (EPOLL_CTL_DEL == op)
        {
            return epoll_ctl(_epoll_fd, op, fd, nullptr);
        }
        else
        {
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = fd;
            return epoll_ctl(_epoll_fd, op, fd, &ev);
        }
    }

private:
    int _epoll_fd; // epoll模型文件描述符
};
